/*
 * @Description: 使用错误处理组件
 * @Author: your name
 * @Date: 2019-08-17 13:53:22
 * @LastEditTime: 2019-08-17 14:42:53
 * @LastEditors: Please set LastEditors
 */
var connect = require('connect')

connect().use(function(){
    foo()
    res.setHeader('Content-type','text/plain')
    res.end('Hello world')
}).use(errorHandler()).listen(3000)



function errorHandler(){
    const env = process.env.NODE_ENV || 'development'
    console.log(process.env.NODE_ENV)
    return function(err,req,res,next){
        res.statusCode = 500
        switch(env){
            case 'development':
                res.setHeader('Content-type','application/json')
                res.end(JSON.stringify(err));
            default:
                res.end('Server error')
        }
    }
}


